<script setup>
import {ref} from 'vue';
import {onMounted} from 'vue'
import api from '../../api/api.js'
// 定义数据(列表元素)
const articles = ref([
    {
        aid: 0,
        articleTitle: "标题",
        views: 0
    }
])

//调用接口获取数据
const getArticles = async () => {
	try {
		const res=await api.getMissionListApi({})
		
		articles.value=res.data.datalist
	} catch (error) {
		console.log("获取数据失败", error)
	}
}

// 删除数据
const deleteData=async (aid) => {
    try {
		await api.deleteArticleApi({aid:aid})
		await getArticles()
		truncateTitles()
	} catch (error) {
		console.log("删除失败", error)
	}
}
async function truncateTitles() {
  let length = articles.value.length;
  for (let i = 0; i < length; i++) {
    if (articles.value[i].articleTitle.length > 60) {
		articles.value[i].articleTitle = articles.value[i].articleTitle.substring(0, 57) + '...'
    }
  }
}


onMounted(async () => {
    try {
		await getArticles()
		truncateTitles()
	} catch (error) {
		console.log(error)
	}
})

</script>
<template>
	<div class="container">
		<div class="card-list">
			<div class="card" v-for="(item,index) in articles" :key="index">
				<div class="card-content">
					<div class="card-title">{{ item.articleTitle }}</div>
					<div class="card-info">ID: {{ item.aid }}, 浏览量: {{ item.views }}</div>
				</div>
				<button class="delete-button" @click="deleteData(item.aid)">删除</button>
			</div>
		</div>
	</div>
</template>

<style scoped lang="less">
.container {
	margin: 0;
	padding: 0;
	height: 100%;
	width: 100%;
	font-family: Arial, sans-serif;
	background-color: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;

}

.card-list {
	width: 95%;
	margin: 0 auto;
	height: 85vh;
	overflow: auto;
}

.card {
	display: flex;
	justify-content: space-between;
	align-items: center;
	width: 100%;
	height: 50px;
	background-color: #ffffff;
	border-radius: 8px;
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	overflow: hidden;
	position: relative;
	margin-bottom: 10px;
}

.card-content {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: flex-start;
	width: calc(100% - 60px);
	/* 60px is for the delete button */
	padding: 0 15px;
	box-sizing: border-box;
}

.card-title {
	font-size: 16px;
	font-weight: bold;
	color: #333333;
}

.card-info {
	font-size: 14px;
	color: #666666;
}

.delete-button {
	background-color: #409eff;
	color: #ffffff;
	border: none;
	border-radius: 4px;
	padding: 5px 10px;
	font-size: 14px;
	cursor: pointer;
	transition: background-color 0.3s ease;
	margin-right: 10px;
}

.delete-button:hover {
	background-color: #2e86de;
	cursor: pointer;
}
</style>
